package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2025/2/22
 */
@Slf4j
@LiteflowComponent(value = "PutAgvAsk4Server", name = "[轮对/轮轴]AGV放料状态查询")
@RequiredArgsConstructor
public class PutAgvAsk4Server extends NodeComponent {

    private final RestTemplate restTemplate;

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;
    @Override
    public void process() throws Exception {
        int i=0;

        LiteFlowUtil.checkInterruptFlag();
        FlowContextDto dto =  this.getRequestData();
        Robot robot = robotService.getDefaultRobot();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String sourceName = Optional.ofNullable(ticketsDto.getModalData().get("sourcePosName"))
                .orElse(ticketsDto.getModalData().get("sourcePosCode")).toString();
        String targetName = Optional.ofNullable(ticketsDto.getModalData().get("targetPosName"))
                .orElse(ticketsDto.getModalData().get("targetPosCode")).toString();

        String auto = ticketsDto.getModalData().get("auto")== null ? "0":
                String.valueOf(ticketsDto.getModalData().get("auto"));

        if (auto.equals("0")) {
            log.info("半自动任务:");
            return ;
        }

        RobotMapPos robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                .eq(RobotMapPos::getName, targetName)
                .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        if (robotMapPos == null) {
            robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                    .eq(RobotMapPos::getCode, targetName)
                    .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        }
        while (true) {
            //String key = FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(0);
            //String uri = this.getCmpData(cn.hutool.json.JSONObject.class).get(key, String.class);
            LiteFlowUtil.checkInterruptFlag();
            try {
                String uri = "http://192.10.13.20:9999/sb/getSBZT?sbbh=01N03";
                // "http://192.10.13.20:9999/sb/getSBZT?sbbh=\"01N03\""
                uri  = uri.replace("01N03", robotMapPos.getName());
                log.info("确认可以放料的请求是:" + uri);
                String result = restTemplate.getForObject(
                        uri, String.class);
                log.info("确认可以放料的请求结果是:" + result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.containsKey("data") && "1".equals(jsonObject.getString("data"))) {
                    log.info("可以上料:");
                    break;
                }
                if (i <=3) {
                    // 请求放料
                    String putAgvStatus = "http://192.10.13.20:9999/lzms/agvPut?status=1&sbbh=5401&sbbh_wz=01N03";
                    putAgvStatus = putAgvStatus.replace("5401", robot.getRobotName()).replace("01N03", targetName);
                    log.info("请求放料的数据的链接是:" + putAgvStatus);
                    String putResult = restTemplate.postForObject(
                            putAgvStatus, ""
                            , String.class);
                    log.info("请求放料的结果:" + putResult);
                    TimeUnit.SECONDS.sleep(5);
                } else {
                    i =0;
                }
                i++;
            }catch (Exception e) {
                log.info("请求进入数据发生异常:" + e);
                TimeUnit.SECONDS.sleep(5);
            }

        }
    }
}
